import hashlib 
import json
import modules.appconfig
import collections
from  util.singelMode  import singleton
@singleton
class CHash:
    def __init__(self):
        pass 
    def __call__(self):
        return self
    @staticmethod
    def getBlockHash(block):
        """
        block dto.block.CBlock
        """
        block = CHash.transferBlockToOrderedDict(block)
        block['selfhash'] = ''
        return CHash.hash(block)

    @staticmethod 
    def getTransactionHash(tx):
        '''
        @tx  python object dict
        '''
        tx['selfhash'] = ""
        tx = CHash.transferTransactionsToOrderedDict(tx)
        return CHash.hash(tx)

    @staticmethod
    def getCopyRightContentHash(conent):
        block_string = json.dumps(conent,ensure_ascii=False)
        block_string=block_string.replace(": ",":")
        block_string=block_string.replace(", ",",")
        return block_string,hashlib.sha256(block_string.encode("utf-8")).hexdigest()
    @staticmethod
    def ifCopyRightContentHashIsRight(contentJsonStr,contentHash):
        if hashlib.sha256(contentJsonStr.encode("utf-8")).hexdigest()==contentHash:
            return True
        else:
            return False


       
    @staticmethod
    def hash(value):
        block_string = json.dumps(value)
        block_string=block_string.replace(": ",":")
        block_string=block_string.replace(", ",",")
        return hashlib.sha256(block_string.encode("utf-8")).hexdigest()
    @staticmethod
    def transferTransactionsToOrderedDict(tx):
       
        orderedtx = collections.OrderedDict()
        orderedtx['version'] = tx['version']
        orderedtx['txtype'] = tx['txtype']
        orderedtx['selfhash'] = tx['selfhash']
        orderedtx['timestamp'] = tx['timestamp']
        orderedtx['txins']=[]
        orderedtx['txouts'] = []
        for txin in tx['txins']:
            orderedtxin = collections.OrderedDict()
            orderedtxinprevout = collections.OrderedDict()
            orderedtxinprevout['txhash'] = txin['prevout']['txhash']
            orderedtxinprevout['outindex'] = txin['prevout']['outindex']
            orderedtxin['prevout'] = orderedtxinprevout

            orderedtxinscriptSig = collections.OrderedDict()
            orderedtxinscriptSig['scriptsig'] = txin['scriptSig']['scriptsig']
            orderedtxinscriptSig['pubkey'] = txin['scriptSig']['pubkey']
            orderedtxinscriptSig['scriptType'] = txin['scriptSig']['scriptType']
            orderedtxin['scriptSig'] = orderedtxinscriptSig
            orderedtx['txins'].append(orderedtxin)

        for txout in tx['txouts']:
            orderedtxout = collections.OrderedDict()
            orderedtxout['value'] = txout['value']
            orderedtxout['content'] = txout['content']
            orderedtxoutscriptPubkey = collections.OrderedDict()
            orderedtxoutscriptPubkey['scriptPubkey'] = txout['scriptPubKey']['scriptPubkey']
            orderedtxoutscriptPubkey['sigtype'] = txout['scriptPubKey']['sigtype']
            orderedtxout['scriptPubKey']=orderedtxoutscriptPubkey
            orderedtx['txouts'].append(orderedtxout)
        return orderedtx
    
    @staticmethod
    def transferBlockToOrderedDict(block):
        orderedBlock = collections.OrderedDict()
        orderedBlock['version'] = block.version
        orderedBlock['timestamp'] = block.timestamp
        orderedBlock['prevBlockHash'] = block.prevBlockHash
        orderedBlock['height'] = block.height
        orderedBlock['proof'] = block.proof
        orderedBlock['selfhash'] = block.selfhash
        orderedBlock['txcounts']=block.txcounts
        orderedBlock['transactions'] = []
        for tx in block.transactions:
            orderedtx = CHash.transferTransactionsToOrderedDict(tx)
            orderedBlock['transactions'].append(orderedtx)
        return orderedBlock









